package com.solution.service;

import java.util.Iterator;
import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import org.apache.log4j.Logger;
import org.jboss.annotation.ejb.RemoteBinding;

import com.solution.constant.ErrorMessages;
import com.solution.constant.ExamErrorMessages;
import com.solution.constant.StatusConstants;
import com.solution.dao.AnswerTypeDAO;
import com.solution.dao.DAOFactory;
import com.solution.exception.DAOException;
import com.solution.exception.ServiceBeanException;
import com.solution.model.AnswerType;
import com.solution.util.SearchVO;

@Stateless
@Remote(AnswerTypeService.class)
@RemoteBinding(jndiBinding="AnswerTypeService")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class AnswerTypeServiceImpl implements AnswerTypeService{

	/**
	 * DAO instance.
	 */
	private AnswerTypeDAO answerTypeDao = (AnswerTypeDAO)DAOFactory.getDAOInstance(AnswerTypeDAO.class.getName());
	
	/**
	 * Logger instance.
	 */
	private Logger log = Logger.getLogger(AnswerTypeServiceImpl.class.getName());

	public List<AnswerType> fetchActiveAnswers(Long questionTypeId) throws ServiceBeanException{
		log.debug("Entering :: AnswerTypeServiceImpl:fetchAnswers()");
		try{
			if(questionTypeId != null){
				SearchVO searchVO = new SearchVO();
				searchVO.addEqualsCondition("questionType.id", questionTypeId, true);
				searchVO.addEqualsCondition("status", StatusConstants.ACTIVE);
				return answerTypeDao.search(searchVO);
			}
			else{
				throw new ServiceBeanException(ErrorMessages.MANDATORY_PARAMETER_MISSING_EXCEPTION+" questionTypeId");
			}
		}
		catch(DAOException ex){
			log.error(ExamErrorMessages.ANSWER_READ_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	public AnswerType fetchCorrectAnswer(Long questionTypeId) throws ServiceBeanException{
		log.debug("Entering :: AnswerTypeServiceImpl:fetchAnswers()");
		try{
			if(questionTypeId != null){
				SearchVO searchVO = new SearchVO();
				searchVO.addEqualsCondition("questionType.id", questionTypeId, true);
				searchVO.addEqualsCondition("isCorrect", true);
				searchVO.addEqualsCondition("status", StatusConstants.ACTIVE);
				return answerTypeDao.searchUnique(searchVO);
			}
			else{
				throw new ServiceBeanException(ErrorMessages.MANDATORY_PARAMETER_MISSING_EXCEPTION+" questionTypeId");
			}
		}
		catch(DAOException ex){
			log.error(ExamErrorMessages.ANSWER_READ_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
	
	public AnswerType createAnswer(AnswerType answer) throws ServiceBeanException{
		log.debug("Entering :: AnswerTypeServiceImpl:createAnswer()");
		try{
			// Perform business validations.
			// check for the mandatory parameters.
			if(answer.checkForMandatoryParameters() != null && answer.checkForMandatoryParameters().size() > 0){
				String errorMsg = ErrorMessages.MANDATORY_PARAMETER_MISSING_EXCEPTION;
				Iterator<String> iter = answer.checkForMandatoryParameters().iterator();
				while(iter.hasNext()){
					errorMsg = errorMsg+", "+iter.next();
				}
				throw new ServiceBeanException(errorMsg);
			}
			else{
				// Create the answer in database.
				answer = answerTypeDao.create(answer);
				log.info("The new answer created with ID : "+answer.getId().longValue());
				log.debug("Exiting :: AnswerTypeServiceImpl:createAnswer()");
				return answer;
			}
		}
		catch(DAOException ex){
			log.error(ExamErrorMessages.ANSWER_CREATE_EXCEPTION, ex);
			throw new EJBException(ex);
		}
	}
}
